<?php
/**
 * 小程序API入口文件
 * 提供API路由和文档
 */

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit;
}

$action = $_GET['action'] ?? 'docs';

if ($action === 'docs') {
    // API文档
    $docs = [
        'name' => '博物馆小程序API',
        'version' => '1.0.0',
        'description' => '提供小程序所需的所有后端接口服务',
        'base_url' => '/backend/api/miniapp/',
        'authentication' => [
            'type' => 'Bearer Token',
            'header' => 'Authorization: Bearer {token}',
            'description' => '大部分接口需要用户登录后获取的token'
        ],
        'endpoints' => [
            'auth' => [
                'description' => '用户认证相关接口',
                'file' => 'auth.php',
                'apis' => [
                    'POST auth.php?action=login' => '微信登录',
                    'GET auth.php?action=profile' => '获取用户信息',
                    'PUT auth.php?action=profile' => '更新用户信息',
                    'POST auth.php?action=refresh' => '刷新token'
                ]
            ],
            'content' => [
                'description' => '内容展示相关接口',
                'file' => 'content.php',
                'apis' => [
                    'GET content.php?action=chapters' => '获取篇章列表',
                    'GET content.php?action=chapter&id={id}' => '获取篇章详情',
                    'GET content.php?action=chapter_exhibits&chapter_id={id}' => '获取篇章下的展项',
                    'GET content.php?action=exhibits' => '获取展项列表',
                    'GET content.php?action=exhibit&id={id}' => '获取展项详情',
                    'GET content.php?action=exhibit_items&exhibit_id={id}' => '获取展项下的展品',
                    'GET content.php?action=items' => '获取展品列表',
                    'GET content.php?action=item&id={id}' => '获取展品详情',
                    'GET content.php?action=media&id={id}' => '获取多媒体文件'
                ]
            ],
            'reservation' => [
                'description' => '预约相关接口',
                'file' => 'reservation.php',
                'apis' => [
                    'GET reservation.php?action=timeslots&date={date}' => '获取可预约时段',
                    'POST reservation.php?action=create' => '创建个人预约',
                    'GET reservation.php?action=list' => '获取用户预约列表',
                    'GET reservation.php?action=detail&id={id}' => '获取预约详情',
                    'PUT reservation.php?action=cancel&id={id}' => '取消预约',
                    'POST reservation.php?action=team_create' => '创建团队预约',
                    'GET reservation.php?action=team_list' => '获取团队预约列表'
                ]
            ],
            'activity' => [
                'description' => '活动相关接口',
                'file' => 'activity.php',
                'apis' => [
                    'GET activity.php?action=list' => '获取活动列表',
                    'GET activity.php?action=detail&id={id}' => '获取活动详情',
                    'POST activity.php?action=apply&activity_id={id}' => '报名活动',
                    'GET activity.php?action=my_applications' => '获取用户报名记录',
                    'GET activity.php?action=application_detail&id={id}' => '获取报名详情',
                    'PUT activity.php?action=cancel&id={id}' => '取消报名',
                    'GET activity.php?action=categories' => '获取活动分类'
                ]
            ],
            'carousel' => [
                'description' => '轮播图相关接口',
                'file' => 'carousel.php',
                'apis' => [
                    'GET carousel.php?action=list' => '获取轮播图列表',
                    'GET carousel.php?action=detail&id={id}' => '获取轮播图详情',
                    'POST carousel.php?action=click&id={id}' => '记录轮播图点击'
                ]
            ],
            'system' => [
                'description' => '系统功能接口',
                'file' => 'system.php',
                'apis' => [
                    'GET system.php?action=config' => '获取系统配置',
                    'GET system.php?action=closures' => '获取闭馆日期',
                    'POST system.php?action=volunteer_apply' => '提交志愿者申请',
                    'GET system.php?action=volunteer_applications' => '获取志愿者申请记录',
                    'POST system.php?action=feedback' => '提交意见反馈',
                    'GET system.php?action=feedback' => '获取反馈记录',
                    'GET system.php?action=faq' => '获取常见问题',
                    'GET system.php?action=faq_categories' => '获取FAQ分类',
                    'POST system.php?action=visit' => '记录访问统计'
                ]
            ]
        ],
        'response_format' => [
            'success' => [
                'code' => 200,
                'success' => true,
                'message' => '操作成功',
                'data' => '具体数据',
                'timestamp' => '时间戳'
            ],
            'error' => [
                'code' => '错误码',
                'success' => false,
                'message' => '错误信息',
                'data' => null,
                'timestamp' => '时间戳'
            ]
        ],
        'common_parameters' => [
            'pagination' => [
                'page' => '页码，默认1',
                'limit' => '每页数量，默认20，最大100'
            ],
            'search' => [
                'search' => '搜索关键词',
                'status' => '状态筛选',
                'category' => '分类筛选'
            ]
        ],
        'status_codes' => [
            200 => '成功',
            400 => '请求参数错误',
            401 => '未授权/登录过期', 
            404 => '资源不存在',
            500 => '服务器内部错误'
        ]
    ];
    
    echo json_encode($docs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    
} elseif ($action === 'test') {
    // API测试接口
    echo json_encode([
        'success' => true,
        'message' => '小程序API服务正常',
        'server_time' => date('Y-m-d H:i:s'),
        'version' => '1.0.0'
    ], JSON_UNESCAPED_UNICODE);
    
} else {
    // 无效操作
    http_response_code(404);
    echo json_encode([
        'success' => false,
        'message' => '接口不存在'
    ], JSON_UNESCAPED_UNICODE);
}
?>